#!/usr/bin/env python
# coding=UTF-8
'''
Author: user
Date: 2021-05-19 19:32:37
LastEditors: user
LastEditTime: 2021-05-19 19:35:06
Descripttion: 
'''
# coding=utf-8
import os
import os.path
import xml.dom.minidom
 
path = r"/home/wnj/projects/Annotations"
files = os.listdir(path)  # 得到文件夹下所有r文件名称
s = []
for xmlFile in files:
    # 遍历文件夹
    portion = os.path.splitext(xmlFile)
    if not os.path.isdir(xmlFile):
        # 判断是否是文件夹,不是文件夹才打开
 
        # 将获取的xml文件名送入到dom解析
        dom = xml.dom.minidom.parse(os.path.join(path, xmlFile))
        ###最核心的部分os.path.join(path,xmlFile),路径拼接,输入的是具体路径
        root = dom.documentElement
        name = root.getElementsByTagName('name')
        # pose=root.getElementsByTagName('pose')
        # 重命名class name
        for i in range(len(name)):
            # print (name[i].firstChild.data)
            # print(xmlFile)
            # if portion[1] == ".xml":
            #     newname = portion[0] + ".jpg"
            #     print(newname)
            newname ="cola"
            if name[i].firstChild.data =="kl":
 
                name[i].firstChild.data = newname
                print(name[i].firstChild.data)
 
            # 保存修改到xml文件中
        with open(os.path.join(path, xmlFile), 'w', encoding='UTF-8') as fh:
            dom.writexml(fh)
            print('修改filename OK!')